package com.ly.mpayment.persist.task;

import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.BillOrder;
import com.ly.mpayment.persist.entity.DownShopInfo;
import com.ly.mpayment.persist.entity.UpShopInfo;
import com.ly.mpayment.persist.service.bill.BillOrderService;
import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
import com.ly.mpayment.persist.service.upshopinfo.IUpShopInfoService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Component("billTaskService")
public class BillTask {

    @Autowired
    IDownShopInfoService downShopInfoService;

    @Autowired
    IUpShopInfoService upShopInfoService;

    @Autowired
    BillOrderService billOrderService;
    /**
     * 每月2号凌晨执行
     */
//    @Scheduled(cron = "0 0 1 1 * ?")
    public void billTask(){
        queryDown("","","");
        queryUp("","","");
    }

    /**
     * 日期为空取当前时间
     * @param startTime
     * @param endtTime
     */
    public void queryDown(String startTime,String endtTime,String useruuid){
        Map<String, Object> parameters = MapUtil.getMap();
        List<DownShopInfo> downShopInfos = null;
        if(StringUtils.isBlank(useruuid)){
            downShopInfos = downShopInfoService.findShopList(parameters);
        }else{
            downShopInfos = new ArrayList<>();
            downShopInfos.add(this.downShopInfoService.findShopInfoByUuid(useruuid));
        }

        if(CollectionUtils.isEmpty(downShopInfos)){
            return;
        }

        for(DownShopInfo shopInfo : downShopInfos){
            if(shopInfo == null){
                continue;
            }
            //查询账单
            JSONObject jsonObject = downShopInfoService.findByBillUserId(shopInfo.getUser().getUseruuid(),startTime,endtTime);
            if(jsonObject.containsKey("username")){
                //写入文件
                String path = ExportExcelUtil.exportDownBill(jsonObject,shopInfo,startTime,endtTime);

                BillOrder order = new BillOrder();
                order.setCreateTime(DateUtils.getNowDate());
                order.setName(jsonObject.getString("username"));
                order.setPath(CookieRedis.get("download").concat(File.separator)+path.replace(CookieRedis.get("exportPath"), ""));
                order.setShopId(shopInfo.getUser().getUseruuid());
                order.setType(1);
                order.setFileType(Config.bill);
                billOrderService.saveBill(order);
            }
        }
    }
    public void queryUp(String startTime,String endtTime,String useruuid){
        List<UpShopInfo> upShopInfos = null;
        if(StringUtils.isBlank(useruuid)){
            upShopInfos =  upShopInfoService.findAllByProvince();
        }else{
            upShopInfos = new ArrayList<>();
            upShopInfos.add(this.upShopInfoService.findByUserId(useruuid));
        }

        if(CollectionUtils.isEmpty(upShopInfos)){
            return;
        }

        for(UpShopInfo upShopInfo : upShopInfos){
            if(upShopInfo == null){
                continue;
            }
            //查询账单
            JSONObject jsonObject = upShopInfoService.findByBillUserId(upShopInfo.getUser().getUseruuid(),startTime,endtTime);
            if(jsonObject.containsKey("username")){
                //写入文件
                String path = ExportExcelUtil.exportUpBill(jsonObject,upShopInfo,startTime,endtTime);

                BillOrder order = new BillOrder();
                order.setCreateTime(DateUtils.getNowDate());
                order.setName(jsonObject.getString("username"));
                order.setPath(CookieRedis.get("download").concat(File.separator)+path.replace(CookieRedis.get("exportPath"), ""));
                order.setShopId(upShopInfo.getUser().getUseruuid());
                order.setType(2);
                order.setFileType(Config.bill);
                billOrderService.saveBill(order);
            }
        }
    }
}
